package com.example.demo.controller;

import com.example.demo.Service.BookService;
import com.example.demo.model.Book;
import com.example.demo.mapper.BookMapper;
import com.example.demo.model.Borrow;
import com.example.demo.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


//书籍相关接口
@RestController
@RequestMapping("/book")
public class BookController {


    @Autowired
    BookService bookService;

    //获取所有书籍列表
    @RequestMapping("list")
    public JsonData getBookList(){

        List<Book> result = bookService.getBookList();
        if(result != null){
            return JsonData.buildSuccess(result);
        }else {
            return JsonData.buildError("获取书籍信息失败");
        }
    }

    //修改书籍信息
    @PostMapping("/modify")
    public JsonData modifyBook(@RequestBody Book newbook){
        Integer result = bookService.modifyBook(newbook);
        if(result != null){
            return JsonData.buildSuccess(result);
        }else {
            return JsonData.buildError("修改图书信息失败");
        }

    }


    //添加新书
    @PostMapping("add")
    public JsonData addBook(@RequestBody Book newbook){
        Integer result = bookService.addBook(newbook);
        if(result != null){
            return JsonData.buildSuccess(result);
        }else {
            return JsonData.buildError("信息失败");
        }
    }


    @PostMapping("delete")
    public JsonData deleteBook(@RequestBody Map<String, Integer> book){
        Integer result = bookService.deleteBook(book.get("book_id"));
        if(result != null){
            return JsonData.buildSuccess(result);
        }else {
            return JsonData.buildError("获取借阅信息失败");
        }
    }
    //获取书籍种类数量
    @GetMapping("category")
    public JsonData  getBookCountByCategory(){
        List<Map<String, Integer>> result = bookService.getBookCountByCategory();
        if(result != null){
            return JsonData.buildSuccess(result);
        }else {
            return JsonData.buildError("获取借阅信息失败");
        }
    }




}
